import pymongo
import configparser
import csv
import hashlib
import urllib
from urllib.parse import urlparse
from urllib import parse
from ordered_set import OrderedSet

config = configparser.ConfigParser()
config.read('config.ini')
conn_str = config['MongoDB']['connection_string']
mongo_client = pymongo.MongoClient(conn_str)

database = config['MongoDB']['database']
db = mongo_client[database]


# read csv


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'{name} process completed. ')  # Press Ctrl+F8 to toggle the breakpoint.


def read_file_type1(file_path):
    alldict = []
    with open(file_path, encoding="utf-8", mode='r') as csvFile:
        readCSV = csv.reader(csvFile)
        # skip header
        # next(readCSV)

        for row in readCSV:
            tmpdict = {}
            for i in range(row.__len__()):
                val = row[i].strip()
                if bool(val):
                    tmpdict[val.lower()] = val.lower()

            for key in tmpdict.keys():
                md5 = hashlib.md5(key.encode('utf-8')).hexdigest().lower()
                synonyms = list(tmpdict.keys())
                synonyms.remove(key)

                alldict.append({'id': md5, 'key': key, 'synonyms': synonyms})
            pass

    return alldict


def read_file_type2(file_path):
    alldict = []
    with open(file_path, encoding="utf-8", mode='r') as csvFile:
        readCSV = csv.reader(csvFile)
        # don't skip header
        # next(readCSV)

        for row in readCSV:
            tmpdict = {}
            topkey = row[0].strip().lower()

            for i in range(row.__len__()):
                val = row[i].strip()
                if bool(val):
                    tmpdict[val.lower()] = val.lower()

            for key in tmpdict.keys():
                md5 = hashlib.md5(key.encode('utf-8')).hexdigest().lower()
                synonyms = list(tmpdict.keys())
                synonyms.remove(topkey)

                alldict.append({'id': md5, 'key': key, 'fj_name': topkey, 'cn_dialectic': synonyms}) ##中医辩证
            pass

    return alldict


def read_file_type3(file_path):
    alldict = []
    with open(file_path, encoding="utf-8", mode='r') as csvFile:
        readCSV = csv.reader(csvFile)
        # don't skip header
        # next(readCSV)

        current_key = ''
        for row in readCSV:
            tmpdict = {}
            if bool(row[0].strip().lower()):
                current_key = row[0].strip().lower()

            for i in range(row.__len__()):
                val = row[i].strip()
                if bool(val):
                    tmpdict[val.lower()] = val.lower()

            for key in tmpdict.keys():
                md5 = hashlib.md5(key.encode('utf-8')).hexdigest().lower()
                synonyms = list(tmpdict.keys())
                if tmpdict.get(current_key) is not None:
                    synonyms.remove(current_key)

                alldict.append({'id': md5, 'key': key, 'fj_name': current_key, 'cn_diagnosis': synonyms}) ##中医诊断
            pass

    return alldict


def read_file_type4(file_path):
    alldict = []
    with open(file_path, encoding="utf-8", mode='r') as csvFile:
        readCSV = csv.reader(csvFile)
        # header
        next(readCSV)
        tmpdict = {}
        tmpdict2 = {}

        for row in readCSV:
            for i in range(row.__len__()):
                val = row[i].strip()
                if bool(val):
                    tmpdict[val.lower()] = val.lower()

        for key in tmpdict.keys():
            tmpurl = urlparse(key)
            synonyms = []
            if tmpurl.scheme is not None and tmpurl.scheme.strip().__len__() > 0:
                name = tmpurl.path.lstrip('/w/').lower() # a-hospital
                if tmpdict2.get(name) is not None:
                    continue

                md5 = hashlib.md5(name.encode('utf-8')).hexdigest().lower()
                synonyms.append(key)
                tmpdict2[name] = name
                alldict.append({'id': md5, 'key': name, 'src': synonyms})
            else:
                md5 = hashlib.md5(key.encode('utf-8')).hexdigest().lower()
                synonyms = []
                tmpdict2[key.lower()] = key.lower()
                alldict.append({'id': md5, 'key': key.lower(), 'src': synonyms})
        pass

    return alldict


def upsert_to_mongo(alldict):
    for one in alldict:
        one['_id'] = one['id']
        one.pop('id')
        entity = db['keywords'].find_one({'_id': one['_id']})
        if entity is not None:
            if one.get('fj_name') is not None:
                entity['fj_name'] = one['fj_name']

            # merge arraylist
            if one.get('synonyms') is not None:
                tmpset = OrderedSet(one.get('synonyms'))
                if entity['synonyms'] is not None:
                    tmpset.union(OrderedSet(entity['synonyms']))
                entity['synonyms'] = list(tmpset)
            if one.get('cn_diagnosis') is not None:
                tmpset = OrderedSet(one.get('cn_diagnosis'))
                if entity.get('cn_diagnosis') is not None:
                    tmpset.union(OrderedSet(entity['cn_diagnosis']))
                entity['cn_diagnosis'] = list(tmpset)
            if one.get('cn_dialectic') is not None:
                tmpset = OrderedSet(one.get('cn_dialectic'))
                if entity.get('cn_dialectic') is not None:
                    tmpset.union(OrderedSet(entity['cn_dialectic']))
                entity['cn_dialectic'] = list(tmpset)

            entity.pop('_id')
            if one.get('src') is not None:
                tmpset = OrderedSet(one.get('src'))
                if entity.get('src') is not None:
                    tmpset.union(OrderedSet(entity['src']))
                entity['src'] = list(tmpset)
            db['keywords'].update_one({'_id': one['_id']}, {'$set': entity})
        else:
            db['keywords'].update_one({'_id': one['_id']}, {'$set': one}, upsert=True)


if __name__ == '__main__':
    #alldict = read_file_type1('type1.csv')
    #upsert_to_mongo(alldict)
    # alldict = read_file_type2('type2.csv')
    # upsert_to_mongo(alldict)
    # alldict = read_file_type3('type3.csv')
    # upsert_to_mongo(alldict)
    # alldict = read_file_type4('type4.csv')
    # upsert_to_mongo(alldict)

    alldict = read_file_type4('type5.csv')
    upsert_to_mongo(alldict)
    alldict = read_file_type4('type6.csv')
    upsert_to_mongo(alldict)

    print_hi('Keyword Upsert Importer ')
